package com.maigao.util;

import com.maigao.entity.LocationDTO;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.http.HttpServletRequest;

public class IPUtil {
    public static long ip2long(String ip) throws Exception {
        long result = 0;

        String[] nums = ip.split("\\.");

        if (nums.length != 4) {
            throw new Exception("ip格式不正确!");
        }


        for (int i = 0; i < 4; i++) {
            int value = Integer.parseInt(nums[i]);
            if (value > 255 || value < 0) {
                throw new Exception("ip格式不正确！");
            }

            result = result | (value << ((3 - i) * 8));
        }

        return result & (0x00000000FFFFFFFFL);
    }


    public static String long2ip(long address) {
        long d = address & 0xFF;
        long c = (address >> 8) & 0xFF;
        long b = (address >> 16) & 0xFF;
        long a = (address >> 24) & 0xFF;

        return String.format("%d.%d.%d.%d", a, b, c, d);
    }

    public static boolean isPrivateAddress(String ip) throws Exception {
        int[] nums = new int[4];
        String[] values = ip.split("\\.");

        if (values.length != 4) {
            throw new Exception("ip格式不正确!");
        }

        for (int i = 0; i < 4; i++) {
            nums[i] = Integer.parseInt(values[i]);
        }

        if (nums[0] == 127) {
            return true;
        }

        if (nums[0] == 10) {
            return true;
        }

        if ((nums[0] == 172) && ((nums[1] & 0xF0) == 16)) {
            return true;
        }

        if ((nums[0] == 192) && (nums[1] == 168)) {
            return true;
        }

        return false;
    }

    public static void main(String[] args) {
        System.out.println(getAddressByIp("61.158.146.176"));
//        try {
//            System.out.println(long2ip(3232263484L));
//            System.out.println(ip2long("192.168.109.60"));
//
//            System.out.println(isPrivateAddress("127.0.0.1"));
//            System.out.println(isPrivateAddress("10.0.0.1"));
//            System.out.println(isPrivateAddress("172.16.0.1"));
//            System.out.println(isPrivateAddress("172.32.0.1"));
//            System.out.println(isPrivateAddress("192.168.0.1"));
//
//            int i = 16 & 0xF0;
//            System.out.println(i);
//
//            i = 17 & 0xF0;
//            System.out.println(i);
//
//        } catch (Exception e) {
//
//        }

    }


    public static LocationDTO queryIpInfo(String ip) {
        if (StringUtils.isEmpty(ip)) {
            return null;
        }
        String url = String.format("http://api.map.baidu.com/location/ip?ak=ULc6GGLAffN1Gq1IyywxwvaadBBya9R4&ip=%s&coor=bd09ll", ip);
        String content = HttpUtils.doGet(url);
        LocationDTO locationDTO = JsonUtil.toBean(content, LocationDTO.class);
        return locationDTO;
    }

    public static String getAddressByIp(String ip) {
        LocationDTO locationDTO = queryIpInfo(ip);
        if (locationDTO == null || locationDTO.getStatus() != 0) {
            return "";
        }
        return locationDTO.getContent().getAddress();
    }

    public static String getProvinceByIp(String ip) {
//        return "";
        try {
            LocationDTO locationDTO = queryIpInfo(ip);
            return locationDTO.getContent().getAddressDetail().getProvince();
        } catch (Exception ex) {
            return "";
        }
    }

    public static String getRequestIP(HttpServletRequest request) {

        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("X-Real-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
